class CreateBasicSchema < ActiveRecord::Migration
  def self.up
    create_table :petrinets do |t|
      t.column :name, :string
      t.column :pid, :string #id from pnml
    end
    
    create_table :transitions do |t|
      t.column :name, :string
      t.column :guard, :string
      t.column :role, :string
      t.column :petrinet_id, :integer
      t.column :manual_execution, :boolean, :default => true
      t.column :xslt_url, :string
      t.column :context_place_id, :string
      t.column :output_model, :text
      t.column :output_form, :text
      t.column :output_bindings, :text
      t.column :pid, :string #id from pnml
      t.column :afterdeploy, :boolean, :default => true
      t.column :action, :string
    end    
    
    create_table :places do |t|
      #type for single table inheritance
      t.column :type, :string
      
      t.column :name, :string
      t.column :pid, :string #id from pnml
      t.column :petrinet_id, :integer
      
      #for non-standard places
      t.column :feed_url, :string #required if push or pull
      
      #for pull places
      t.column :consumed_tokens, :text
      t.column :etag, :string
      t.column :last_modified, :datetime
      t.column :contenttype, :string
      t.column :modelxml, :string
    end
    
    create_table :locators do |t|
      t.column :place_id, :integer
      t.column :name, :string
      t.column :expr, :string
      t.column :val_type, :string
    end
  
    create_table :tokens do |t|
      t.column :place_id, :integer
      t.column :value, :text
      t.column :cached_locators, :text
      t.column :uuid, :string
      t.column :case_id, :integer
      t.column :is_active, :boolean, :default => true
      t.column :transition_mode_id, :integer
    end

    create_table :arcs do |t|
      #type for single table inheritance
      t.column :type, :string
      t.column :from_is_place, :boolean
      t.column :petrinet_id, :integer
      t.column :transition_id, :integer
      t.column :place_id, :integer
      t.column :xslt_url, :string
      t.integer :weight, :default => 1, :null => false
      t.string :weight_xpath
      t.column :pid, :string #id from pnml
      t.column :case_transformation, :integer #see PNEngine::Arc
    end

    create_table :combinations do |t|
      t.column :transition_id, :integer
    end

    create_table :modtokens do |t|
      t.column :transition_id, :integer
      t.column :token_id, :integer
    end

    create_table :combinations_tokens, :id => false do |t|
      t.column :combination_id, :integer, :null => false
      t.column :token_id, :integer, :null => false
    end
    
    create_table :cases do |t|
      t.column :name, :string
      t.column :parent_id, :integer
    end
    
    create_table :roles do |t|
      t.column :name, :string
      t.column :tr_initProcess, :bool, :default => true
      t.column :allocate, :bool, :default => true
      t.column :suspend, :bool, :default => true
      t.column :resume, :bool, :default => true
      t.column :submit, :bool, :default => true
      t.column :review, :bool, :default => true
      t.column :delegate, :bool, :default => true
      t.column :skip, :bool, :default => true
    end
    
    #initialize roles Student, Mitarbeiter, Professor, Bewerber, Boss, Administrator 
    Role.create :name => "Student", :tr_initProcess => false, :allocate => true, :suspend => true, :resume => true, :submit => true, :review => false, :delegate => false, :skip => false 
    Role.create :name => "Mitarbeiter", :tr_initProcess => true, :allocate => true, :suspend => true, :resume => true, :submit => true, :review => true, :delegate => true, :skip => false
    Role.create :name => "Professor", :tr_initProcess => true, :allocate => true, :suspend => true, :resume => true, :submit => true, :review => true, :delegate => true, :skip => true
    Role.create :name => "Bewerber", :tr_initProcess => false, :allocate => true, :suspend => true, :resume => true, :submit => true, :review => false, :delegate => false, :skip => false
    Role.create :name => "Boss", :tr_initProcess => true, :allocate => true, :suspend => true, :resume => true, :submit => true, :review => true, :delegate => true, :skip => true
    Role.create :name => "Administrator", :tr_initProcess => true, :allocate => true, :suspend => true, :resume => true, :submit => true, :review => true, :delegate => true, :skip => true
    Role.create :name => "Default", :tr_initProcess => true, :allocate => true, :suspend => true, :resume => true, :submit => true, :review => true, :delegate => true, :skip => true
    
    create_table :users do |t|
      t.column :name, :string
    end
    
    #initialize users
    User.create :name => "Lutz"
    User.create :name => "Matthias"
    User.create :name => "Stefan"
    User.create :name => "Frau Heinrich"
    User.create :name => "Prof. Dr. Weske"
    User.create :name => "Frau Pamperin"
    User.create :name => "Hartz4"
    User.create :name => "Chef"
    User.create :name => "Admin"
    User.create :name => "Sekretaerin"
    User.create :name => "testuser"
      
    create_table :roles_users, :id => false do |t|
      t.column :role_id, :integer, :null => false
      t.column :user_id, :integer, :null => false
    end
    
    create_table :transition_modes do |t|
      t.column :transition_id, :integer
      t.column :execution_container_id, :integer
      t.column :execution_container_type, :string
    end
    
    create_table :tokens_transition_modes, :id => false do |t|
      t.column :transition_mode_id, :integer, :null => false
      t.column :token_id, :integer, :null => false
    end
    
    create_table :transition_modes_transition_modes, :id => false do |t|
      t.column :transition_mode_id, :integer, :null => false
      t.column :preceding_mode_id, :integer, :null => false
    end
    
    create_table :execution_logs do |t|
      t.column :case_id, :integer
    end
    
    create_table :execution_contexts do |t|
      t.column :case_id, :integer
    end
    
    #initialize connection betweeen users and roles
    lutz = User.find(:first, :conditions => ["name = 'Lutz'"] )
    matthias = User.find(:first, :conditions => ["name = 'Matthias'"] )
    stefan = User.find(:first, :conditions => ["name = 'Stefan'"] )
    frauHeinrich = User.find(:first, :conditions => ["name = 'Frau Heinrich'"] )
    profWeske = User.find(:first, :conditions => ["name = 'Prof. Dr. Weske'"] )
    frauPamperin = User.find(:first, :conditions => ["name = 'Frau Pamperin'"] )
    hartz4 = User.find(:first, :conditions => ["name = 'Hartz4'"] )
    chef = User.find(:first, :conditions => ["name = 'Chef'"] )
    admin = User.find(:first, :conditions => ["name = 'Admin'"] )
    sekretaerin = User.find(:first, :conditions => ["name = 'Sekretaerin'"])
    testuser = User.find(:first, :conditions => ["name = 'testuser'"])
    
    student = Role.find(:first, :conditions => ["name = 'Student'"])
    mitarbeiter = Role.find(:first, :conditions => ["name = 'Mitarbeiter'"])
    professor = Role.find(:first, :conditions => ["name = 'Professor'"])
    bewerber = Role.find(:first, :conditions => ["name = 'Bewerber'"])
    boss = Role.find(:first, :conditions => ["name = 'Boss'"])
    administrator = Role.find(:first, :conditions => ["name = 'Administrator'"])
    default = Role.find(:first, :conditions => ["name = 'Default'"])
    
    lutz.roles << student
    matthias.roles << student
    stefan.roles << student
    frauHeinrich.roles << mitarbeiter
    frauPamperin.roles << mitarbeiter
    profWeske.roles << professor
    hartz4.roles << bewerber
    chef.roles << boss
    admin.roles << administrator
    sekretaerin.roles << mitarbeiter
    testuser.roles << default
    
  end
  
  def self.down
    drop_table :petrinets
    drop_table :tokens
    drop_table :locators
    drop_table :transitions
    drop_table :places
    drop_table :arcs
    drop_table :combinations
    drop_table :modtokens
    drop_table :combinations_tokens
    drop_table :cases
    drop_table :users
    drop_table :roles
    drop_table :roles_users
    drop_table :transition_modes
    drop_table :tokens_transition_modes
    drop_table :transition_modes_transition_modes
    drop_table :execution_logs
    drop_table :execution_contexts
  end
end
